package hydrap2p.library;

import hydrap2p.helper.ServiceConnector;
import hydrap2p.logging.Level;
import hydrap2p.logging.Logging;

import org.apache.thrift.TException;

public class LibraryLogging {

	private static Logging.Client logger;
	private static long lastCheck=0;
	private static final int checkOften=10000; 
	public static void log(Level level, String source, String reason)
	{
		synchronized(LibraryLogging.class)
		{
			if (logger==null && System.currentTimeMillis()>(lastCheck+checkOften))
			{
				logger=ServiceConnector.getLogger();
				lastCheck=System.currentTimeMillis();
			}
			if (logger==null)
			{
				System.err.println(source+": "+reason);
				return;
			}
			
			try {
				logger.log(level, source, reason);
			} catch (TException e) {
				System.err.println(source+": "+reason);
				logger=null;
			}
		}
	}
	public static void setLogger(Logging.Client logger)
	{
		LibraryLogging.logger=logger;
	}
}
